//
//  CCFinderViewController.m
//  CwinCar
//
//  Created by dlios on 15/10/15.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "CCFinderViewController.h"
#import "BaseHeader.h"
#import "CCFindTableViewCell.h"
#import "CCFindDetailCell.h"
#import "CCFindModel.h"
#import "CCFindDetailModel.h"
#import "CCFindDetailViewController.h"
#import "HeaderViewControllerViewController.h"
#import "SearchViewController.h"
@interface CCFinderViewController ()<UITableViewDataSource , UITableViewDelegate,findTableViewDelegate>
{
   
       
    UITableView *_tableView;
    NSInteger didSelete ;
    NSInteger sectionDid ;
    
}
@property (nonatomic , strong) NSMutableArray *fcArray ;
@property (nonatomic, strong)NSMutableArray *infoArray;// 存放每辆车
@property (nonatomic, strong)NSMutableArray *sectionArray;// 按字母分类
@property (nonatomic, strong)NSMutableArray *titleSectionArray;
@property (nonatomic , strong) NSMutableArray * mArr;
@property (nonatomic, strong)NSMutableArray *carsArray;// 存放详情car的section

@property (nonatomic, strong)NSMutableArray *allCarsArray;// 存放详情car中每一辆car
@property (nonatomic , strong) NSMutableArray * allCoArray ;

@property (nonatomic , strong) NSMutableArray * collArray ;

@property (nonatomic , strong) NSMutableArray * coseArray ;

@property (nonatomic , strong) UILabel * titleLabel ;


@end

@implementation CCFinderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.titleLabel setTextColor:[UIColor whiteColor]];
    self.title = @"找车" ;
    // 搜索按钮
    
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchButton setFrame:CGRectMake( 335 *suitableScreen_Width, 10 *suitableScreen_Width , 20 * suitableScreen_Width, 20 *suitableScreen_Width)];
    [searchButton setBackgroundImage:[UIImage imageNamed:@"iconfont-search.png"] forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(searchButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:searchButton];
#pragma mark 主界面tableView
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, mainScreen_Width, mainScree_Height - 110 * suitableScreen_Height ) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.sectionIndexColor = [UIColor grayColor];
    // 详情tableView
#pragma mark - 点击下面弹出的tableView
    _carsTableView = [[UITableView alloc]initWithFrame:CGRectMake(mainScreen_Width, 0, mainScreen_Width, mainScree_Height  - 49 * suitableScreen_Width)];
    _carsTableView.delegate = self ;
    _carsTableView.dataSource = self ;
    [self.view addSubview:_carsTableView];
    
    //点击collectionView
#pragma makr - 点击collection弹出的tableView
    _coTableView = [[UITableView alloc]initWithFrame:CGRectMake(mainScreen_Width, 147 * suitableScreen_Width, mainScreen_Width, 517 *suitableScreen_Width - 110 * suitableScreen_Width)];
    
    _coTableView.delegate = self ;
    
    _coTableView.dataSource = self ;
    
    [self.view addSubview:_coTableView];
    
    [self getData:_tableView];
}

- (void)searchButtonClick:(UIButton *)sender
{
    SearchViewController *search = [[SearchViewController alloc] init];
    
    [self presentViewController:search animated:YES completion:^{
        
    }];
    
    
}

#pragma mark - 网络请求
- (void)getData:(UITableView *)tableView
{
    
    _infoArray = [[NSMutableArray alloc] init];
    _sectionArray = [[NSMutableArray alloc] init];
    _titleSectionArray = [[NSMutableArray alloc] init];
    
    //获得右边的导航栏
    [AFNblock getdataWithString:@"http://mrobot.pcauto.com.cn/v2/price/brands?v=4.0.0" block:^(id block) {
        for (NSDictionary *dic in [block objectForKey:@"sections"]) {
            
            for (NSDictionary *d in [dic objectForKey:@"brands"]) {
                CCFindModel *model = [CCFindModel modelWithDictionary:d];
                [_infoArray addObject:model];
                
            }
            // 获得右侧导航栏
            [_titleSectionArray addObject:[dic objectForKey:@"index"]];
            [_sectionArray addObject:dic];
        }
        [tableView reloadData];
        
    }];
    
    _mArr = [[NSMutableArray alloc] init];
    //collectionView里的数据
//    [ AFNblock getdataWithString:@"http://app.api.autohome.com.cn/autov4.8.8/dealer/hotbrands-pm1.json" block:^(id block) {
//        
//        for (NSDictionary *dic in [[block objectForKey:@"result"] objectForKey:@"list"]) {
//            CCFindModel *model = [CCFindModel modelWithDictionary:dic];
//            [_mArr addObject:model];
//        }
//        [tableView reloadData];
//    }];
//    
    
}



#pragma mark - tableView Delegate&DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:_tableView]) {
        
        return [[[_sectionArray objectAtIndex:section] objectForKey:@"brands"] count];
    }
    else if ([tableView isEqual:_carsTableView])
    {
        
        return [[[_allCarsArray objectAtIndex:section] objectForKey:@"carsArr"] count];
    }
    
    else if ([tableView isEqual:_coTableView])
    {
        return [[[_allCoArray objectAtIndex:section] objectForKey:@"cArray"] count];
    }
    return 0;
}

#pragma mark - tableViewCell创建

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    if ([tableView isEqual:_tableView]) {
        
        static NSString *cellId = @"cell";
        
        FindTableViewCell   *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[FindTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        for (NSDictionary *dic in [[_sectionArray objectAtIndex:[indexPath section]] objectForKey:@"brands"]) {
            CCFindModel *model = [CCFindModel modelWithDictionary:dic];
            
            [arr addObject:model];
        }
        if (arr.count!=0) {
            
        CCFindModel *model = [arr objectAtIndex:indexPath.row];
        [cell getInfo:model];
        }
#pragma mark 代理人签给cell
        cell.delegate = self ;
        return cell;
    }
    
    else
    {
        static NSString *cellId2 = @"cellId";
        CCFindDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId2];
        if (!cell) {
            cell = [[CCFindDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId2];
        }
        //只有传数据的时候不同
        if ([tableView isEqual:_carsTableView]) {
            
            if (_allCarsArray.count!=0) {
        
            CCFindDetailModel *model = [[[_allCarsArray objectAtIndex:[indexPath section]] objectForKey:@"carsArr"] objectAtIndex:indexPath.row];
            [cell getInfo:model];
            }
        }
        
        else
        {
            if (_allCoArray!=0) {
                
            CCFindHeadModel * model= [[[_allCoArray objectAtIndex:[indexPath section]] objectForKey:@"cArray"] objectAtIndex:indexPath.row];
            
            [cell getCo:model];
            }
        }
#pragma mark 右滑手势
        
        if (_carsTableView.center.x < mainScreen_Width ) {
            UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(pushRight)];
            [swipe setDirection:UISwipeGestureRecognizerDirectionRight];
            [cell addGestureRecognizer:swipe];
            
            [_carsTableView addGestureRecognizer:swipe];
        }
        
        if (_coTableView.center.x < mainScreen_Width) {
            UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(coRight)];
            [swipe setDirection:UISwipeGestureRecognizerDirectionRight];
            [cell addGestureRecognizer:swipe];
            [_coTableView addGestureRecognizer:swipe];
        }
        
        return cell;
    }
    
    
    
    return nil ;
    
}

#pragma mark - 滑上面弹出来的

-(void)coRight
{
    [UIView animateWithDuration:0.3 animations:^{
        
        [_coTableView setFrame:CGRectMake(mainScreen_Width, 147 * suitableScreen_Width, mainScreen_Width,  517 * suitableScreen_Width - 110 )];
        
        
    }];}
#pragma 滑下面弹出来的

-(void)pushRight
{
    
    [UIView animateWithDuration:0.3 animations:^{
        
        [_carsTableView setFrame:CGRectMake(mainScreen_Width, 0, mainScreen_Width, mainScree_Height - 113 * suitableScreen_Width )];
        
        
    }];
    
}
#pragma mark- collection代理弹出的方法

-(void)giveUrl:(NSString *)url
{
    
    
    [UIView animateWithDuration:0.3 animations:^{
        
        [_coTableView setFrame:CGRectMake(0, 147 * suitableScreen_Width, mainScreen_Width,  517 * suitableScreen_Width - 110)];
        _coTableView.hidden = NO ;
        
    }];
    
    
    [AFNblock getdataWithString:url block:^(id block) {
        
        NSMutableDictionary * reDic = [block objectForKey:@"result"];
        _fcArray = [reDic objectForKey:@"fctlist"];
        _carsArray = [[NSMutableArray alloc] init];
        _allCoArray = [[NSMutableArray alloc] init];
        _coseArray = [[NSMutableArray alloc]init];
        _collArray = [[NSMutableArray alloc]init];
        for (NSMutableDictionary * dic in _fcArray) {
            
            NSString * name = [dic objectForKey:@"name"];
            
            [_coseArray addObject:name];
            
            NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] init];
            [_allCoArray addObject:mutableDic];
            
            
            for (NSDictionary * dd in [dic objectForKey:@"serieslist"]) {
                
                CCFindHeadModel * model = [CCFindHeadModel modelWithDictionary:dd];
                [_collArray addObject:model];
                
            }
            [mutableDic setObject:_collArray forKey:@"cArray"];
            
            
        }
        [_coTableView reloadData];
        _coTableView.hidden = NO ;
        _carsTableView.hidden = YES ;
    }];
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1 ;
}

#pragma mark tableView 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([tableView isEqual:_tableView]) {
        
        if (indexPath.section != 0 ) {
            
            
            
            [_coTableView setFrame:CGRectMake(mainScreen_Width, 147 * suitableScreen_Width, mainScreen_Width,  517 * suitableScreen_Width - 64 * suitableScreen_Width)];
            _coTableView.hidden = YES ;
            
            [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionTransitionCurlUp animations:^{
                [_carsTableView setFrame:CGRectMake(mainScreen_Width / 3 * suitableScreen_Width, 0, mainScreen_Width, mainScree_Height - 113)];
                [_carsTableView reloadData];
                _carsTableView.hidden = NO ;
                
                
                
            } completion:nil];
            
        }
        
        //
#pragma mark -  详情页数据解析
        
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        for (NSDictionary *dic in [[_sectionArray objectAtIndex:[indexPath section]] objectForKey:@"brands"]) {
            CCFindModel *model = [CCFindModel modelWithDictionary:dic];
            model.cid = [dic objectForKey:@"id"];
            [arr addObject:model];
        }
        CCFindModel *model = arr[indexPath.row];
        
        
        [AFNblock getdataWithString:[NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v3/price/modelLibraryHomePage?bid=%@", model.cid] block:^(id block) {
            
            _carsArray = [[NSMutableArray alloc] init];
            _allCarsArray = [[NSMutableArray alloc] init];
            
            
            for (NSDictionary *dic in [block objectForKey:@"manufacturers"])
            {
                CCFindDetailModel *model = [CCFindDetailModel modelWithDictionary:dic];
                [_carsArray addObject:model];
                
                // 创建字典 存放汽车serials
                
                NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] init];
                
                
                // 将汽车serials装入大数组
                [_allCarsArray addObject:mutableDic];
                
                // 创建存放详情汽车的数组
                NSMutableArray *mutableArr = [[NSMutableArray alloc] init];
                
                for (NSDictionary *dd in [dic objectForKey:@"serials"]) {
                    CCFindDetailModel *mm = [CCFindDetailModel modelWithDictionary:dd];
                    // 将详情汽车model装入数组
                    [mutableArr addObject:mm];
                }
                // 将详情汽车数组装入字典
                [mutableDic setObject:mutableArr forKey:@"carsArr"];
                
            }
            
            [_carsTableView reloadData];
            _carsTableView.hidden = NO ;
            _coTableView.hidden = YES ;
        }];
        
    }
    else if ([tableView isEqual:_carsTableView])
    {
        CCFindDetailViewController * fView = [[CCFindDetailViewController alloc]init];
        
        CCFindDetailModel *model = [[[_allCarsArray objectAtIndex:[indexPath section]] objectForKey:@"carsArr"] objectAtIndex:indexPath.row];
        fView.model = model ;
        
        [self.navigationController pushViewController:fView animated:YES];
        
    }
    else
    {
        HeaderViewControllerViewController *header = [[HeaderViewControllerViewController alloc]init];
        CCFindHeadModel * model= [[[_allCoArray objectAtIndex:[indexPath section]] objectForKey:@"cArray"] objectAtIndex:indexPath.row];
        header.model = model;
        
        header.hidesBottomBarWhenPushed = YES ;
        
        [self.navigationController pushViewController:header animated:YES];
        
    }
}
#pragma mark - tableView  datasource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:_carsTableView]) {
        return 90 * suitableScreen_Width;
    }
    
    if ([tableView isEqual:_coTableView]) {
        return 90 * suitableScreen_Width;
    }
    if ([tableView isEqual:_tableView]) {
        
        if ([indexPath section] == 0) {
            return 130 * suitableScreen_Width;
        }
        
        
        return 40 *suitableScreen_Width;
    }
    return 0 ;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if ([tableView isEqual:_tableView]) {
        
        return _sectionArray.count;
    }
    else if ([tableView isEqual:_carsTableView])
    {        return _carsArray.count;}
    else
        return _coseArray.count ;
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if ([tableView isEqual:_tableView]) {
        
        return [_titleSectionArray objectAtIndex:section];
    }
    else if ([tableView isEqual:_carsTableView])
    {
        CCFindModel *model = [_carsArray objectAtIndex:section];
        return model.name;
    }
    else if ([tableView isEqual:_coTableView])
    {
        NSString * str = [_coseArray objectAtIndex:section];
        return str ;
    }
    return @"haha";
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    
    if ([tableView isEqual:_tableView]) {
        
        
        
        return _titleSectionArray;
    }
    return nil ;
    

    
  


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
